class CodeIndex
  def initialize(project)
    @project = project
  end
    
  def update
    FileUtils.rm_f tags_file
    @project.chdir do
      `#{e_sh ctags_bin} -f #{e_sh tags_file} -R --fields=Kn --excmd=pattern --languages=Ruby --exclude=@#{e_sh exclude_patterns} .`
    end
  end
  
  def locations_for(entry)
    ret = []
    File.open(tags_file, 'r') do |file|
      file.each_line do |line|
        if line =~ /^#{entry}\t/
          entry_name, path, pattern, type, line_no = line.split("\t")
          line_no = line_no.split(":").last.to_i
          ret << SourceLocation.new(@project, path, line_no)
        end
      end
    end
    ret
  end
  
  private
  
  def ctags_bin
    File.join(EVIL_ROOT, 'bin', 'ctags')
  end
  
  def tags_file
    @project.expand_path('tmtags')
  end
  
  def exclude_patterns
    @project.ignore_setting_file
  end
end